package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.AreaService;
import com.dongdongshop.service.CityService;
import com.dongdongshop.service.ProvinceService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

@Controller
@RequestMapping("address")
public class AddressController {
    private Logger logger = LoggerFactory.getLogger(AddressController.class);
    @Reference
    private AddressService addressService;
    @Reference
    private ProvinceService provinceService;
    @Reference
    private CityService cityService;
    @Reference
    private AreaService areaService;

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(){
        try {
            List<AddressVo> list = addressService.getinfo();
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),list);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("getProvince")
    @ResponseBody
    public dataResult getProvince(){
        try {
            List<TbProvinces> list = provinceService.getProvince();
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),list);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("getCity")
    @ResponseBody
    public dataResult getCity(String provinceid){
        try {
            List<TbCities> list = cityService.getCity(provinceid);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),list);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("getArea")
    @ResponseBody
    public dataResult getArea(String cityid){
        try {
            List<TbAreas> list = areaService.getArea(cityid);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),list);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("addinfo")
    @ResponseBody
    public dataResult addinfo(TbAddress tbAddress){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            tbAddress.setUserId(user.getUsername());
            tbAddress.setIsDefault("0");
            Integer i = addressService.addinfo(tbAddress);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAddress")
    @ResponseBody
    public dataResult deleteAddress(Long id){
        try {
            Integer i = addressService.deleteAddress(id);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id){
        try {
            TbAddress tbAddress = addressService.getById(id);
            List<TbProvinces> list = provinceService.getProvince();
            Map<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("t",tbAddress);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbAddress tbAddress){
        try {
            Integer i = addressService.updateinfo(tbAddress);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

}
